package com.snopy.designPattern.create.prototype;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Patient
 * @description
 * @email 77912204@qq.com
 * @date 2022/03/01 21:34
 */
@Getter
@Setter
public class User implements Cloneable{
    private String name;
    private String gender;
    private Integer age;
    private String addr;
    private List<Address> recAddr;
    private Order order;
    @Override
    public User clone() {
        try {
            User user = (User) super.clone();
            user.setOrder((Order)user.getOrder().clone());
            List<Address> list = new ArrayList<>();
            for (Address addr:user.getRecAddr()) {
                list.add((Address) addr.clone());
            }
            user.setRecAddr(list);
            // TODO: copy mutable state here, so the clone can't change the internals of the original
            return user;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", addr='" + addr + '\'' +
                ", recAddr=" + (recAddr.size()>0?recAddr.get(0).toString():"") +
                ", order=" + order.toString() +
                '}';
    }
}
